package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IPlatformUserService;
import com.mdd.admin.validate.PlatformUserCreateValidate;
import com.mdd.admin.validate.PlatformUserUpdateValidate;
import com.mdd.admin.validate.PlatformUserSearchValidate;
import com.mdd.admin.vo.PlatformUserListedVo;
import com.mdd.admin.vo.PlatformUserDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.PlatformUser;
import com.mdd.common.mapper.PlatformUserMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 平台用户实现类
 * @author LikeAdmin
 */
@Service
public class PlatformUserServiceImpl implements IPlatformUserService {
        
    @Resource
    PlatformUserMapper platformUserMapper;

    /**
     * 平台用户列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<PlatformUserListedVo>
     */
    @Override
    public PageResult<PlatformUserListedVo> list(PageValidate pageValidate, PlatformUserSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<PlatformUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        platformUserMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:usersName@users_name:str",
        });

        IPage<PlatformUser> iPage = platformUserMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<PlatformUserListedVo> list = new LinkedList<>();
        for(PlatformUser item : iPage.getRecords()) {
            PlatformUserListedVo vo = new PlatformUserListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 平台用户详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return PlatformUser
     */
    @Override
    public PlatformUserDetailVo detail(Integer id) {
        PlatformUser model = platformUserMapper.selectOne(
                new QueryWrapper<PlatformUser>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        PlatformUserDetailVo vo = new PlatformUserDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 平台用户新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(PlatformUserCreateValidate createValidate) {
        PlatformUser model = new PlatformUser();
        model.setUsersName(createValidate.getUsersName());
        model.setContacts(createValidate.getContacts());
        model.setPhone(createValidate.getPhone());
        model.setEmail(createValidate.getEmail());
        model.setUserTypeId(createValidate.getUserTypeId());
        model.setUserType(createValidate.getUserType());
        model.setCreatesTime(createValidate.getCreatesTime());
        model.setUpdateUserId(createValidate.getUpdateUserId());
        model.setUpdateUserName(createValidate.getUpdateUserName());
        model.setUserId(createValidate.getUserId());
        model.setUserName(createValidate.getUserName());
        platformUserMapper.insert(model);
    }

    /**
     * 平台用户编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(PlatformUserUpdateValidate updateValidate) {
        PlatformUser model = platformUserMapper.selectOne(
                new QueryWrapper<PlatformUser>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUsersName(updateValidate.getUsersName());
        model.setContacts(updateValidate.getContacts());
        model.setPhone(updateValidate.getPhone());
        model.setEmail(updateValidate.getEmail());
        model.setUserTypeId(updateValidate.getUserTypeId());
        model.setUserType(updateValidate.getUserType());
        model.setCreatesTime(updateValidate.getCreatesTime());
        model.setUpdateUserId(updateValidate.getUpdateUserId());
        model.setUpdateUserName(updateValidate.getUpdateUserName());
        model.setUserId(updateValidate.getUserId());
        model.setUserName(updateValidate.getUserName());
        platformUserMapper.updateById(model);
    }

    /**
     * 平台用户删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        PlatformUser model = platformUserMapper.selectOne(
                new QueryWrapper<PlatformUser>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        platformUserMapper.updateById(model);
    }

}
